import {
    NextRequest,
    NextResponse
} from 'next/server';
import {
    prisma
} from '@/lib/db';
import {
    emailRegex,
    passwordRegex
} from '@/lib/regexp';
import bcrypt from 'bcryptjs';
import {
    createTokens,
    setAuthCookies
} from '@/lib/jwt';


export async function POST(req: NextRequest) {
    try {
        const {
            email,
            password
        } = await req.json();
        if (!email || !emailRegex.test(email)) {
            return NextResponse.json({
                error: '邮箱格式错误'
            }, {
                status: 400
            })
        }
        if (!password || !passwordRegex.test(password)) {
            return NextResponse.json({
                error: '密码需要6-18位，包含字母、数字和特殊字符'
            }, {
                status: 400
            })
        }
        const user = await prisma.user.findUnique({
            where: {
                email
            }
        });
        if (!user) {
            return NextResponse.json({
                error: '用户不存在'
            }, {
                status: 401
            })
        }
        const isPasswordValid = await bcrypt.compare(password, user.password);
        if (!isPasswordValid) {
            return NextResponse.json({
                error: '密码错误'
            }, {
                status: 401
            })
        }
        const {
            accessToken,
            refreshToken
        } = await createTokens(user.id);
        
        await prisma.user.update({
            where: {
                id: user.id
            },
            data: {
                refreshToken
            }
        })

        setAuthCookies(accessToken, refreshToken);

        return NextResponse.json({
            message: '登录成功'
        })
    } catch (error) {
        console.log(error);
        return NextResponse.json({
            error: '登录失败'
        }, {
            status: 500
        })
    } finally {
        // 释放数据库对象
        await prisma.$disconnect();
    }
}